package ngames.com.sushitv.action;

import android.app.Activity;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.adapter.DemoAdapter;
import ngames.com.sushitv.adapter.VideoRankAdapter;
import ngames.com.sushitv.base.BaseAction;
import ngames.com.sushitv.base.BaseListAction;
import ngames.com.sushitv.core.ModelCallback;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.interfaces.OnItemClickListener;
import ngames.com.sushitv.temp.model.RankData;

/**
 * recycleView的重用
 */
public class VideoRankListAction extends BaseListAction {
    private String mDataID = "0";
    private RecyclerView mRecycler;
    private VideoRankAdapter mAdapter;
    //private LinearLayoutManager mLinearLayoutManager;
    private int RecyclerId;
    //private List<RankData> rankDataList;
    private int columnNum;//列数
    private String total;//总数
    private String videoType;
    public VideoRankListAction(Activity activity, View baseView, int id, int columnNum, String total, String videoType) {
        super(activity, baseView);
        this.RecyclerId=id;
        //this.rankDataList=rankDataList;
        this.columnNum=columnNum;
        this.total=total;
        this.videoType=videoType;
    }
    @Override
    public void loadMore() {

    }
    @Override
    public void onCreate() {
        super.onCreate();
        mRecycler = (RecyclerView) mBaseView.findViewById(RecyclerId);
       /* if(num!=null) {
            mLinearLayoutManager = new LinearLayoutManager(mActivity);
        }else{

        }*/

        //每个item高度一致，可设置为true，提高性能
        mRecycler.setHasFixedSize(true);
        //mRecycler.setLayoutManager(mLinearLayoutManager);
        mRecycler.setLayoutManager(new GridLayoutManager(mActivity, columnNum));
        videoRank();
    }

    @Override
    public void onRefreshData() {
        videoRank();
    }

    private void videoRank() {
        getAppModel().videoRank(videoType, total, null, new ModelCallback<ArrayList<ItemVideo>>() {
            @Override
            public void onBeforeRequest(String urlTag) {

            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> data) {
                mIsLoadingMore = false;
                //mSwipeRefreshLayout.setRefreshing(false);
                if ("0".equals(mDataID) && mAdapter != null) {
                    mAdapter.clearData();
                }
                if (isEmptyList(data)) {
                    return;
                }
                mDataID = data.get(data.size() - 1).getId();
                setListData(data);
            }

            @Override
            public void onFailure(String errorEvent, String message) {
                Log.e("fail",errorEvent+"\n"+message);
            }
        });
    }


    public void setListData(ArrayList<ItemVideo> data) {
        if (mAdapter == null) {
            mAdapter = new VideoRankAdapter(mActivity, data);
            mAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }

                @Override
                public void onItemButtonClick(View view, int position) {

                }
            });
            mRecycler.setAdapter(mAdapter);
        } else {
            mAdapter.addData(data);
        }
    }
}
